package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsUserBusinessAuthDao;
import com.syni.mdd.yhd.common.all.entity.BmsUserBusinessAuth;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsUserBusinessAuthService extends BaseService<BmsUserBusinessAuth, Integer> {

    final static int BUSINESS_AUTH = 1;
    //已认领
    final static int BUSINESS_CLAIMED_STATUS = 2;

    @Autowired
    BmsUserBusinessAuthDao bmsUserBusinessAuthDao;

    @Override
    public BmsUserBusinessAuthDao getBaseDao() {
        return bmsUserBusinessAuthDao;
    }

    //检验店铺是否完成资质认证
    public boolean checkBusinessIsAuth(Integer businessId){
        boolean ret = false;
        BmsUserBusinessAuth bmsUserBusinessAuth = this.findByBusinessIdNotReject(businessId);
        if(bmsUserBusinessAuth != null){
            if(bmsUserBusinessAuth.getBmsBusiness().getIsAuth().intValue() == Constants.COMMON_FLAG_TRUE_INT){
                return true;
            }
        }
        return ret;
    }

    public BmsUserBusinessAuth findByUserId(Integer userId){
        Set userIds = new HashSet();
        userIds.add(userId);
        List<BmsUserBusinessAuth> bmsUserBusinessAuths = this.findByUserIds(userIds);
        if(bmsUserBusinessAuths != null && bmsUserBusinessAuths.size() > 0){
            return bmsUserBusinessAuths.get(0);
        }
        return null;
    }

    public List<BmsUserBusinessAuth> findByUserIds(Set userIds){
        List<BmsUserBusinessAuth> bmsUserBusinessAuths = new LinkedList<>();
        if(userIds.size() > 0) {
            bmsUserBusinessAuths.addAll(bmsUserBusinessAuthDao.findByBmsUserIdIn(userIds));
        }
        return bmsUserBusinessAuths;
    }

    public BmsUserBusinessAuth findByUserIdNotReject(Integer userId,Integer businessId){
        List<BmsUserBusinessAuth>  bmsUserBusinessAuths = bmsUserBusinessAuthDao.findByBmsUserId(userId);
        if(bmsUserBusinessAuths != null && bmsUserBusinessAuths.size() > 0){
            for(BmsUserBusinessAuth bmsUserBusinessAuth : bmsUserBusinessAuths) {
                if(bmsUserBusinessAuth.getBmsBusinessId() != null) {
                    if (bmsUserBusinessAuth.getBmsBusinessId().intValue() == businessId.intValue()) {
                        return bmsUserBusinessAuth;
                    }
                }
            }
        }
        return null;
    }

    public List<BmsUserBusinessAuth> findAllUserBusiness(Integer userId){
        return bmsUserBusinessAuthDao.findByBmsUserId(userId);
    }

    public List<BmsUserBusinessAuth> findUserClaimBusiness(Integer userId){
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("bmsUserId",userId);
        List<BmsUserBusinessAuth>  bmsUserBusinessAuths = bmsUserBusinessAuthDao.findAll(new SpecificationUtils(conditionMap));
        return bmsUserBusinessAuths;
    }

    public BmsUserBusinessAuth findByBusinessIdNotReject(Integer businessId){
        List<BmsUserBusinessAuth>  bmsUserBusinessAuths = bmsUserBusinessAuthDao.findByBmsBusinessId(businessId);
        if(bmsUserBusinessAuths != null && bmsUserBusinessAuths.size() > 0){
            return bmsUserBusinessAuths.get(0);
        }
        return null;
    }

    public boolean checkUserIsAuthBusinessId(Integer userId,Integer businessId){
        List<BmsUserBusinessAuth>  bmsUserBusinessAuths = bmsUserBusinessAuthDao.findByBmsUserId(userId);
        if(bmsUserBusinessAuths != null && bmsUserBusinessAuths.size() > 0){
            BmsUserBusinessAuth bmsUserBusinessAuth = bmsUserBusinessAuths.get(0);
            if(businessId.intValue() == bmsUserBusinessAuth.getBmsBusinessId().intValue()){
                return true;
            }
        }
        return false;
    }


    public Integer findBusinessManager(Integer businessId) {
        List<BmsUserBusinessAuth>  bmsUserBusinessAuths = bmsUserBusinessAuthDao.findByBmsBusinessId(businessId);
        if(bmsUserBusinessAuths != null && bmsUserBusinessAuths.size() > 0){
            return bmsUserBusinessAuths.get(0).getBmsUserId();
        }
        return null;
    }

    public List<BmsUserBusinessAuth> findAllBusinessByUserId(Integer userId) {
        return bmsUserBusinessAuthDao.findByBmsUserId(userId);
    }

    public Page<BmsUserBusinessAuth> findUserBusinessByPageable(Integer userId, Pageable pageable) {
        return bmsUserBusinessAuthDao.findByBmsUserId(userId,pageable);
    }
}
